﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Xml.Linq;

namespace WEEK10_LECTURE
{
    public partial class XmlWriterExample01 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnWriterXmlFile_Click(object sender, EventArgs e)
        {
            string filePath = Server.MapPath("EmployeesII.xml");

            try
            {
                XmlWriter writer = XmlWriter.Create(filePath);

                writer.WriteStartDocument(); //写入XML文档声明（<?xml version ……））
                writer.WriteStartElement("employees"); //写入employees起始标签
                writer.WriteStartElement("employee"); //写入employee起始标签
                writer.WriteAttributeString("id", "1"); //写入id="1"属性
                writer.WriteStartElement("name"); //写入name起始标签
                writer.WriteElementString("firstName", "Nancy"); //写入firstName元素
                writer.WriteElementString("lastName", "Davolio"); //写入lastName元素
                writer.WriteEndElement(); //写入name结束标签
                writer.WriteStartElement("city"); //此处将一个元素分为三个部分写入
                writer.WriteString("Seattle"); //写入Seattle文本值
                writer.WriteEndElement(); //写入city结束标签
                writer.WriteElementString("state", "WA"); //写入city元素
                writer.WriteElementString("zipCode", "98122"); //写入zipCode元素
                writer.WriteEndDocument(); //写入employees结束标签

                writer.Close(); //关闭XmlWriter
                Response.Write("文件创建成功");
            }
            catch (Exception exception)
            {
                Response.Write(exception.Message);
            }
        }
    }
}